using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;
using QFramework.Example;
using JetBrains.Annotations;
using UnityEngine.UIElements;

namespace ProjectSurvivor
{
	public partial class RepeatTileController : ViewController//重复贴图控制器
	{
		//使用一次就注释掉了 用于在 Inspector 面板中添加一个按钮来重新计算 Tilemap 的边界
// #if UNITY_EDITOR
// 		[UnityEditor.CustomEditor(typeof(RepeatTileController))]
// 		public class RepeatTileControllerEditor : UnityEditor.Editor
// 		{
// 			public override void OnInspectorGUI()
// 			{
// 				base.OnInspectorGUI();
// 				if (GUILayout.Button("重新计算 Bounds"))
// 				{
// 					var controller = target as RepeatTileController;
// 					controller.Tilemap.CompressBounds();
// 				}
// 			}
// 		}
// #endif
		private Tilemap mUp;
		private Tilemap mDown;
		private Tilemap mLeft;
		private Tilemap mRight;
		private Tilemap mUpLeft;
		private Tilemap mUpRight;
		private Tilemap mDownLeft;
		private Tilemap mDownRight;
		private Tilemap mCenter;

		private int AreaX = 0;
		private int AreaY = 0;

		void CreateTileMaps()
		{
			mUp = Tilemap.InstantiateWithParent(transform);
			mDown = Tilemap.InstantiateWithParent(transform);
			mLeft = Tilemap.InstantiateWithParent(transform);
			mRight = Tilemap.InstantiateWithParent(transform);
			mUpLeft = Tilemap.InstantiateWithParent(transform);
			mUpRight = Tilemap.InstantiateWithParent(transform);
			mDownLeft = Tilemap.InstantiateWithParent(transform);
			mDownRight = Tilemap.InstantiateWithParent(transform);
			mCenter = Tilemap;
		}
		void UpdatePositions()
		{
			mUp.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mDown.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
			mRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
			mUpLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mUpRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mDownLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mDownRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mCenter.Position(new Vector3((AreaX + 0) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
		}
		void Start()
		{
			CreateTileMaps();
			UpdatePositions();
		}
		void Update()
		{
			if (Player.Default && Time.frameCount % 60 == 0)//每60帧检查一次玩家位置并更新区域
			{
				//获取玩家位置并计算区域         将世界坐标转换为格子坐标
				var cellPos = Tilemap.layoutGrid.WorldToCell(Player.Default.transform.Position());
				// 计算区域坐标
				AreaX = cellPos.x / Tilemap.size.x;
				AreaY = cellPos.y / Tilemap.size.y;
				UpdatePositions();
			}
		}
	}
}